/**
 * @author mithun 
 */

//var console = loadFirebugConsole();

Event.observe(window, 'load', function(event) {
	
	initMediaMediaVersionBoxes();
	
});

var initMediaMediaVersionBoxes = function(){
	
	/*
	disableMediaType($('MediaMediaText'));
	disableMediaType($('MediaMediaImage'));
	disableMediaType($('MediaMediaAudio'));
	disableMediaType($('MediaMediaVideo'));
	*/
	
	toggleYouTubeIntegration($('MediaMediaIsYoutubeIntegrated'));
		
	if($('MediaMediaHasActiveTextVersion').checked){
		switchMediaType('MediaMediaHasActiveTextVersion');
	} else if($('MediaMediaHasActiveImageVersion').checked){
		switchMediaType('MediaMediaHasActiveImageVersion');
	} else if($('MediaMediaHasActiveAudioVersion').checked){
		switchMediaType('MediaMediaHasActiveAudioVersion');
	} else if($('MediaMediaHasActiveVideoVersion').checked){
		switchMediaType('MediaMediaHasActiveVideoVersion');
	}

}

var switchMediaType = function(refElementId){
	
	disableMediaType($('MediaMediaText'));
	disableMediaType($('MediaMediaImage'));
	disableMediaType($('MediaMediaAudio'));
	disableMediaType($('MediaMediaVideo'));
	
	switch(refElementId){
		case 'MediaMediaHasActiveTextVersion':
			if($('MediaMediaHasActiveTextVersion').checked){
				enableMediaType($('MediaMediaText'));
				disableMediaType($('MediaMediaImage'));
				disableMediaType($('MediaMediaAudio'));
				disableMediaType($('MediaMediaVideo'));
				$('MediaMediaHasActiveImageVersion').checked = false;
				$('MediaMediaHasActiveAudioVersion').checked = false;
				$('MediaMediaHasActiveVideoVersion').checked = false;
			} 
			break;
		case 'MediaMediaHasActiveImageVersion':
			if($('MediaMediaHasActiveImageVersion').checked){
				disableMediaType($('MediaMediaText'));
				enableMediaType($('MediaMediaImage'));
				disableMediaType($('MediaMediaAudio'));
				disableMediaType($('MediaMediaVideo'));
				$('MediaMediaHasActiveTextVersion').checked = false;
				$('MediaMediaHasActiveAudioVersion').checked = false;
				$('MediaMediaHasActiveVideoVersion').checked = false;
			}
			break;
		case 'MediaMediaHasActiveAudioVersion':
			if($('MediaMediaHasActiveAudioVersion').checked){
				disableMediaType($('MediaMediaText'));
				disableMediaType($('MediaMediaImage'));
				enableMediaType($('MediaMediaAudio'));
				disableMediaType($('MediaMediaVideo'));
				$('MediaMediaHasActiveTextVersion').checked = false;
				$('MediaMediaHasActiveImageVersion').checked = false;
				$('MediaMediaHasActiveVideoVersion').checked = false;
			}
			break;
		case 'MediaMediaHasActiveVideoVersion':
			if($('MediaMediaHasActiveVideoVersion').checked){
				disableMediaType($('MediaMediaText'));
				disableMediaType($('MediaMediaImage'));
				disableMediaType($('MediaMediaAudio'));
				enableMediaType($('MediaMediaVideo'));
				$('MediaMediaHasActiveTextVersion').checked = false;
				$('MediaMediaHasActiveImageVersion').checked = false;
				$('MediaMediaHasActiveAudioVersion').checked = false;
			}
			break;
	}
	
}

var enableMediaType = function(mediaType){

	mediaType.enable();
	//mediaType.show();

}


var disableMediaType = function(mediaType){

	//mediaType.hide();
	mediaType.disable();

}

var checkForm = function(el) {
	
	if(!($('MediaMediaHasActiveTextVersion').checked || $('MediaMediaHasActiveImageVersion').checked || $('MediaMediaHasActiveAudioVersion').checked || $('MediaMediaHasActiveVideoVersion').checked)){
		alert('Please select atleast one media type');
		return false;
	}

	if($('MediaMediaHasActiveTextVersion').checked && $('MediaMediaText').getValue()==""){
		alert('Please Enter the Text');
		return false;		
	} 
	if($('MediaMediaHasActiveImageVersion').checked && $('MediaMediaImage').getValue()==""){
		alert('Please select an image file');
		return false;		
	} 
	if($('MediaMediaHasActiveAudioVersion').checked && $('MediaMediaAudio').getValue()==""){
		alert('Please select an audio file');
		return false;		
	} 
	if($('MediaMediaHasActiveVideoVersion').checked && $('MediaMediaVideo').getValue()==""){
		alert('Please select a video file');
		return false;		
	} 
	if(!checkMediaCombination()){
		return false;		
	}
	if(!$('MediaMediaHasActiveTextVersion').checked){
		$('MediaMediaText').value = null;
		$('MediaMediaText').hide();
		$('MediaMediaText').enable();
	}
	if(!$('MediaMediaHasActiveImageVersion').checked){
		$('MediaMediaImage').value = null;
		$('MediaMediaImage').hide();
		$('MediaMediaImage').enable();
	}
	if(!$('MediaMediaHasActiveAudioVersion').checked){
		$('MediaMediaAudio').value = null;
		$('MediaMediaAudio').hide();
		$('MediaMediaAudio').enable();
	}
	if(!$('MediaMediaHasActiveVideoVersion').checked){
		$('MediaMediaVideo').value = null;
		$('MediaMediaVideo').hide();
		$('MediaMediaVideo').enable();
	}
	submitFormViaJavaScript(el.form)
	return false;
	
}

var checkMediaCombination = function(){
	
	if($('MediaMediaHasActiveVideoVersion').checked){
		if($('MediaMediaHasActiveAudioVersion').checked){
			alert("Audio and Video media cannot be played at same time\n \t Please unselect one media");
			return false;
		} else if($('MediaMediaHasActiveImageVersion').checked){
			alert("Image and Video media cannot be played at same time\n \t Please unselect one media");
			return false;
		} else if($('MediaMediaHasActiveTextVersion').checked){
			alert("Text and Video media cannot be played at same time\n \t Please unselect one media");
			return false;
		} 
	} else if($('MediaMediaHasActiveAudioVersion').checked){
		if($('MediaMediaHasActiveImageVersion').checked){
			alert("Audio and Image media cannot be played at same time\n \t Please unselect one media");
			return false;
		} else if($('MediaMediaHasActiveTextVersion').checked){
			alert("Audio and Text media cannot be played at same time\n \t Please unselect one media");
			return false;
		} 
	} else if($('MediaMediaHasActiveImageVersion').checked){
		if($('MediaMediaHasActiveTextVersion').checked){
			alert("Text and Image media cannot be played at same time\n \t Please unselect one media");
			return false;
		} 
	}	
	return true;
	
}

var limitAttach = function(type, id, file){
	
	allowExtension = false;
	switch(type){
		case "text":
			extArray = new Array(".txt");
			break;
		case "image":
			extArray = new Array(".gif", ".jpg", ".jpeg", ".png");
			break;
		case "audio":
			extArray = new Array(".mp3");
			break;
		case "video":
			extArray = new Array(".mpg", ".mpeg", ".mp4", ".flv", ".mov", ".mp3", ".wav", ".au", ".ogg");
			break;
		default :
		break;
	}
	if(!file){
		return;
	}
	while(file.indexOf("\\") != -1){
		file = file.slice(file.indexOf("\\") + 1);
	}
	ext = file.slice(file.lastIndexOf(".")).toLowerCase();
	for(var i = 0; i < extArray.length; i++){
		if(extArray[i] == ext){
			allowExtension = true; 
			break; 
		}
	}
	if(!allowExtension){
		alert("Only files with extension "+ 
			(extArray.join(", ")) + 
			" are acceptable as "+ type +
			" file \nPlease select a new file to upload.");
		document.getElementById(id).value="";
	}
	
}

/**
 * Show/hide YouTube integartion options
 * @param {Object} el
 */
var toggleYouTubeIntegration = function(el) {
	
	var youTubeIntegatedRows = document.getElementsByClassName('youTubeIntegated');
	var i = 0;
	
	if (el.checked) {
		for(i = 0; i < youTubeIntegatedRows.length; i++) {
			youTubeIntegatedRows[i].show();
		}
	} else {
		for(i = 0; i < youTubeIntegatedRows.length; i++) {
			youTubeIntegatedRows[i].hide();
		}
	}
}
